﻿using System;
using System.Net;

using Tulde.Core;
using System.Collections.Generic;

namespace Tulde.MP3tunes
{
    public class StorageCache : IViewModelCacheProvider
    {

        #region IViewModelCacheProvider Members

        public bool CacheItemExists(object key)
        {
            return StorageProviderFactory.Instance.Exists(key.ToString());
        }

        public void SetCacheItem(object key, string instance)
        {
            StorageProviderFactory.Instance.Save(key.ToString(), instance);
        }

        public void SetCacheItem<T>(object key, T instance)
        {
            StorageProviderFactory.Instance.Save<T>(key.ToString(), instance);
        }

        public T GetCacheItem<T>(object key)
        {
            string x = StorageProviderFactory.Instance.Read(key.ToString());
            return ViewModelSerializerFactory.Instance.Deserialize<T>(x);
        }

        

        #endregion


        #region IViewModelCacheProvider Members


        public void Invalidate(params string[] paths)
        {
           

            foreach (var item in paths)
            {
                try
                {
                    StorageProviderFactory.Instance.Delete(item.ToString());
                }
                catch
                {

                }
            }
        }

        #endregion
    }
}
